package mini.statement;

import java.util.HashMap;
import java.util.LinkedList;

import mini.Proc;
import mini.StatementList;
import mini.expr.Element;
import mini.expr.Expr;


public class IfStatement extends Statement {

    private Expr expr;
    private StatementList stmtlist1,  stmtlist2;

    public IfStatement(Expr e, StatementList list1, StatementList list2) {
        expr = e;
        stmtlist1 = list1;
        stmtlist2 = list2;
    }

    public IfStatement(Expr e, StatementList list) {
        expr = e;
        stmtlist1 = list;
    }

    public void eval(HashMap<String, Element> nametable, HashMap<String, Proc> functiontable, LinkedList var) throws Exception {
        Element element = expr.eval(nametable, functiontable, var);
        
        if (!element.isInt()) {
        	throw new IllegalArgumentException("Parse error. Expression does not evaluate to an int.");
        }
        
		if (element.getIntValue() > 0) {
            stmtlist1.eval(nametable, functiontable, var);
        } else {
            stmtlist2.eval(nametable, functiontable, var);
        }
    }
}